<template>
	<view class="container">
		<view class="top-banner">
			为了保证您高质量脱单，请您完成实名认证，如您仍有疑虑，可以加水印（仅作为缘诉实名认证实名）后进行认证。
		</view>
		<view class="example-body">
			<view class="realName">
				<view class="name">真实姓名</view>
				<input class="uni-input" ref="input1" v-model="realInfo.realName" placeholder="请输入" />
				<view v-if="realInfo.idCardReal===0" class="audit">
					审核中
				</view>
				<view v-if="realInfo.idCardReal===1" class="audit">
					认证成功 
				</view>
				<view v-if="realInfo.idCardReal===2" class="audit">
					认证未通过
				</view>
			</view>
			<view class="realName idNo">
				<view class="name">身份证号</view>
				<input class="uni-input" ref="input2" v-model="realInfo.idNum" placeholder="请输入身份证号" />
			</view>
			<view class="title">
				身份证正面照（人像面）
			</view>
			<imgUpload :max="1" :action="`${uploadUrl}${state}/`" v-model="realInfo.idCard"
				@uploadSuccess="uploadSuccess1" @imgDelete="imgDelete" :headers="{'Authorization':token}">
			</imgUpload>
			<view class="title">
				身份证反面照（国徽）
			</view>
			<imgUpload :max="1" :action="`${uploadUrl}${state}/`" v-model="realInfo.idCard2"
				@uploadSuccess="uploadSuccess2" @imgDelete="imgDelete2" :headers="{'Authorization':token}">

			</imgUpload>
		</view>
		<view class="btn" @click="post">
			提交
		</view>
	</view>

</template>

<script>
	import {
		url,
		request,
		login
	} from '@/service/api.js'
	import imgUpload from '@/components/htz-image-upload/htz-image-upload.vue'
	export default {
		data() {
			return {
				realName: '',
				images: [],
				token: `Bearer ${uni.getStorageSync('auth')}`,
				state: 0,
				uploadUrl: url.upload,
				realInfo: {
					idCard: [],
					idCard2: [],
					idCardReal: 0,
					idNum: '',
					realName: ''
				}
			};
		},
		mounted() {
			request(url.getRealInfo, 'GET').then(res => {
				if (res.status === 200) {
					this.realInfo = res.data
					this.realInfo.idCard = res.data.idCard ? [res.data.idCard] : []
					this.realInfo.idCard2 = res.data.idCard2 ? [res.data.idCard2] : []
				}
			}).catch(err => {
				uni.showToast({
					title: err.msg
				})
			})
		},
		components: {
			imgUpload: imgUpload
		},
		methods: {
			uploadSuccess1(res) {
				if (res.statusCode === 200) {
					const result = JSON.parse(res.data)
					this.realInfo.idCard.push(result.data)
					console.log(result)
				}
			},
			uploadSuccess2(res) {
				if (res.statusCode === 200) {
					const result = JSON.parse(res.data)
					this.realInfo.idCard2.push(result.data)
					console.log(result)
				}
			},
			uploadFail(e) {
				uni.showToast({
					title: '错误',
					text: '上传图片失败'
				})
			},
			imgDelete(e) {
				this.realInfo.idCard = []
			},
			imgDelete2(e) {
				this.realInfo.idCard2 = []
			},
			post() {
				let param = {
					...this.realInfo,
					idCard: this.realInfo.idCard[0],
					idCard2: this.realInfo.idCard2[0]
				}
				request(url.uploadRealInfo, 'POST', param).then(res => {
					if (res.status === 200) {
						uni.showToast({
							title: '提交成功'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)
					} else {
						uni.showToast({
							icon: 'error',
							title: res.msg
						})
					}
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: err.msg
					})
				})
			}
		}
	}
</script>

<style>
	uni-page-body {
		background-color: #fff !important;
	}
</style>
<style lang="less">
	* {
		box-sizing: border-box;
	}

	.top-banner {
		padding: 15px;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		background: linear-gradient(-41deg, rgba(49, 104, 174, 0.57), rgba(145, 83, 188, 0.57));
		font-size: 13px;
		font-family: SimHei;
		color: #FFFFFF;
	}

	.container {
		padding-bottom: 40px;
		// padding: 18px;
		width: 100%;

		.text {
			height: 100px;
		}
	}

	.example-body {
		font-size: 13px;
		font-family: SimHei;
		color: #888888;
		padding: 18px;

		.realName {
			display: flex;
			align-items: center;
			width: 70%;

			input {
				border-bottom: 1px solid #888;
				// border-radius: 4px;
				margin-left: 10px;
				height: 40px;
			}

			.audit {
				margin-left: 10px;
				color: #8AA9D1;
			}
		}

		.idNo {
			width: 100%;
			margin-top: 20px;
		}

		.title {
			margin-top: 45px;
			margin-bottom: 20px;
		}
	}

	.btn {
		margin: auto;
		margin-top: 40px;
		width: 90%;
		height: 35px;
		background: #FC9380;
		border-radius: 5px;
		font-size: 17px;
		color: #FFFFFF;
		line-height: 23px;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
